﻿using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Ebusiness_UserService.Services;
using Ebusiness_UserService.Services.Dtos.User;
using Ebusiness_UserService.Services.UnitOfWork;
using Ebusiness_UserService.Data;
using Ebusiness_UserService.Model;
using System.Collections.Generic;
using Volo.Abp.Caching;
using Ebusiness_UserService.Redis;
using Microsoft.Extensions.Caching.Distributed;

namespace Ebusiness_UserService.Controllers;


[Route("User/[action]")]
[ApiController]
public class UserController : AbpController, IUserService
{
    public IUserService userService { get; set; }

    public IConfiguration configuration { get; set; }

    public IDistributedCache<BookCacheItem> _cache { get; set; }

    public IDistributedCache<List<BookCacheItem>> _listCache { get; set; }

    public ILogger<UserController> logger { get; set; } 

    public UserController()
    {

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="productDto"></param>
    /// <returns></returns>
    //[UnitOfWork(typeof(Ebusiness_UserServiceDbContext))]
    //[HttpPost]
    //public async Task<UserDto> Create(UserDto productDto)
    //{
    //    var dto = await userService.CreateAsync(productDto);
    //    return dto;
    //}

    /// <summary>
    /// 获取列表数据
    /// </summary>
    /// <returns></returns>
    //[HttpGet]
    //public async Task<List<UserDto>> GetList()
    //{
    //    var dtos = await userService.GetListAsync(new PagedAndSortedResultRequestDto() { MaxResultCount = 50, SkipCount = 0 });
    //    return dtos.Items.ToList();
    //}

    [HttpPost]
    public async Task<string> LoginByPhoneAndPwd(LoginByPhoneAndPwdRequest req)
    {
        Console.WriteLine("查询商品");
        return await userService.LoginByPhoneAndPwd(req);
    }

    [HttpGet]
    public async Task<UserDto> GetAsync(Guid id)
    {
        return await userService.GetAsync(id);
    }

    [HttpPost]
    public async Task<PagedResultDto<UserDto>> GetListAsync(PagedAndSortedResultRequestDto input)
    {
        #region  服务降级使用nacos中心配置
        {
            //服务降级：改动服务接口功能。
            //​ 例如：查询MySQL中商品数据，换成查询redis中的商品数据。
            //​ 例如：查询redis中的商品数据。换成查询MySQL中商品数据
            //​ 服务降级应用场景：大促期间，如果并发大了。换成查询redis。
            //​ 大促已过，如果并发小了。换成查询mysql。
            //​ 核心工具：配置中心
            //​ 前提：电商网站中商品数据存储到redis中。
            //​ 环境：redis准备
            //​ 技术：Volo.Abp.Caching.StackExchangeRedis
            //​ 具体技术：IDistributedCache<List>
            //var w = configuration["nihaoa"];
            //var ww = configuration["shijie"];
           // List<BookCacheItem> bookCacheItems = new List<BookCacheItem>() { new BookCacheItem() { Name = "1233", Price = 123 }, new BookCacheItem() { Name = "1233", Price = 123 } };
           // await _listCache.SetAsync("BookCacheItem:list", bookCacheItems, new DistributedCacheEntryOptions
           // {
           //     AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30)
           // });
           // BookCacheItem bookCacheItem = new BookCacheItem() { Name = "1233", Price = 123 };
           // await _cache.SetAsync(
           //            "BookCacheItem:001",   // Redis Key
           //            bookCacheItem,         // 数据对象
           //new DistributedCacheEntryOptions
           //{
           //    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30)
           //});
        }
        #endregion
        var w = configuration["nihaoa"];
        var ww = configuration["shijie"];
        logger.LogInformation("代码没报错");
        logger.LogError("代码报错啦");
        return await userService.GetListAsync(input);
    }

    [HttpPost]
    public async Task<UserDto> CreateAsync(UserDto input)
    {
        return await userService.CreateAsync(input);
    }

    [HttpPut]
    public async Task<UserDto> UpdateAsync(Guid id, UserDto input)
    {
        return await userService.UpdateAsync(id, input);
    }

    [HttpDelete]
    public async Task DeleteAsync(Guid id)
    {
        await userService.DeleteAsync(id);
    }

    [HttpGet]
    public async Task<List<User>> GetByUserList()
    {
        return await userService.GetByUserList();
    }
}
